package com.wgworkshop.pems.common;

import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.List;

/**
 * 全局异常处理。
 * Created by wlc on 2017/7/2.
 */
@ControllerAdvice
@ResponseBody
public class GlobalExceptionAdvice {
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ResponseData<String> handleHttpMessageNotReadableException(HttpMessageNotReadableException ex) {
        ex.printStackTrace();
        return new ResponseData<String>().failure(HttpStatus.BAD_REQUEST.value(), "请求参数解析失败。", ex.getMessage());
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseData<List<FieldError>> handleMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
        ex.printStackTrace();
        return new ResponseData<List<FieldError>>().failure(HttpStatus.BAD_REQUEST.value(), ex.getMessage(), ex.getBindingResult().getFieldErrors());
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(BusinessException.class)
    public ResponseData<BusinessException> handleBusinessException(BusinessException ex) {
        ex.printStackTrace();
        return new ResponseData<BusinessException>().failure(HttpStatus.BAD_REQUEST.value(), ex.getMessage());
    }

    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResponseData<String> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException ex) {
        ex.printStackTrace();
        return new ResponseData<String>().failure(HttpStatus.METHOD_NOT_ALLOWED.value(), "不支持当前请求方法。", ex.getMessage());
    }

    @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    public ResponseData<String> handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException ex) {
        ex.printStackTrace();
        return new ResponseData<String>().failure(HttpStatus.UNSUPPORTED_MEDIA_TYPE.value(), "不支持当前媒体类型。", ex.getMessage());
    }

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public ResponseData<String> handleException(Exception ex) {
        ex.printStackTrace();
        return new ResponseData<String>().failure(HttpStatus.INTERNAL_SERVER_ERROR.value(), "服务器处理异常。", ex.getMessage());
    }
}
